package com.fyzk.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tyjc.lottery.R;
import com.fyzk.utils.Utils;

@SuppressLint("NewApi")
public class JCTopTitleView extends RelativeLayout implements OnClickListener {

    private Context context;
    // 顶部
    private TextView top_back;
    protected TextView top_title;
    protected ImageView top_filter;
    private ImageView top_popu;


    public JCTopTitleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        init();
        // TODO Auto-generated constructor stub
    }

    public JCTopTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
        // TODO Auto-generated constructor stub
    }

    public JCTopTitleView(Context context) {
        super(context);
        this.context = context;
        init();
        // TODO Auto-generated constructor stub
    }

    private void init() {
        View.inflate(context, R.layout.layout_jc_top, this);
        if (isInEditMode()) {
            return;
        }
        top_back = (TextView) findViewById(R.id.zcbf_title_iv_back);
        top_title = (TextView) findViewById(R.id.zcbf_top_title);
        top_popu = (ImageView) findViewById(R.id.zcbf_title_iv_popu);
        top_filter = (ImageView) findViewById(R.id.zcbf_title_iv_filter);
        top_back.setOnClickListener(this);
        top_title.setOnClickListener(this);
        top_popu.setOnClickListener(this);
        top_filter.setOnClickListener(this);
    }

    public void setCenterText(int resid) {
        top_title.setText(resid);
    }

    public void setCenterText(String text) {
        top_title.setText(text);
    }

    public interface JCTopTitlClickListener {
        void onLeftClick();

        void onFilterClick();

        void onPopBtnClick();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (isInEditMode()) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            ViewGroup.LayoutParams params = this.getLayoutParams();
            params.height = Utils.dip2px(75);
            this.setLayoutParams(params);
            this.setPadding(0, Utils.dip2px(25), 0, 0);
            this.setBackgroundColor(this.getContext().getResources().getColor(R.color.top_bg));
        }
    }

    private JCTopTitlClickListener topListener = null;

    public void setOnJCTopTitlClickListener(JCTopTitlClickListener topListener) {
        this.topListener = topListener;
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (topListener == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.zcbf_title_iv_back:
                topListener.onLeftClick();
                break;
            case R.id.zcbf_title_iv_popu:
                topListener.onPopBtnClick();
                break;
            case R.id.zcbf_title_iv_filter:
                topListener.onFilterClick();
                break;
        }
    }

    public void setRightVisible(int visible) {
        top_filter.setVisibility(visible);
        top_popu.setVisibility(visible);
    }

    public View getPopBtnView() {
        return top_popu;
    }

}
